package com.chainyoung.component_tmperson.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.appcommon.hdwallet.CoinConstant;
import com.chainyoung.common.appcommon.hdwallet.OwnWalletUtils;
import com.chainyoung.common.appcommon.hdwallet.WordsListUtil;
import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.common.view.kprogresshud.KProgressHUD;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonFragmentComponent;
import com.chainyoung.component_tmperson.di.module.PersonFragmentModule;
import com.chainyoung.component_tmperson.ui.mvp.contract.ImportWalletContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.ImportWalletPresenter;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.chainyoung.common.appcommon.hdwallet.CoinConstant.ETH;
import static com.chainyoung.common.constant.CommonStringConstants.IS_GET_WALLET;

public class ImportWalletByPrivateKeyFragment extends BaseFragment<ImportWalletPresenter> implements ImportWalletContract.View {

    @BindView(R2.id.et_private_key)
    EditText etPrivateKey;

    private String coinType = CoinConstant.ETH;//币种
    private HdWalletCache hdWallet;

    public static ImportWalletByPrivateKeyFragment newInstance(String type) {
        ImportWalletByPrivateKeyFragment fragment = new ImportWalletByPrivateKeyFragment();
        Bundle args = new Bundle();
        args.putString("coinType", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null) {
            coinType = getArguments().getString("coinType");
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPersonFragmentComponent.builder()
                .appComponent(appComponent)
                .personFragmentModule(new PersonFragmentModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUi() {
        super.initUi();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_import_hdwallet_by_privatekey;
    }

    public void setPrivateKey(String privateKey) {
        etPrivateKey.setText(privateKey);
    }


    @OnClick(R2.id.tv_save)
    void importWallet() {
        if (TextUtils.isEmpty(etPrivateKey.getText().toString().trim())) {
            ToastUtil.showToast("请输入正确的私钥");
            return;
        }

        hdWallet = OwnWalletUtils.importByPrivateKey(mContext, SPUtil.getString(LibConstant.PIN_CODE), StringUtil.viewGetText(etPrivateKey), coinType);
        if (hdWallet == null) {
            return;
        }
        mPresenter.getAllHdWalletCaches(coinType);

    }




    @Override
    public void onAddHdWalletCacheSuccess(HdWalletCache data) {
        RouterUtils.getInstance()
                .build(RouterConstants.PATH_MAIN_MARKET_ACTIVITY)
                .withInt("page", 3)
                .withBoolean(IS_GET_WALLET, true)
                .navigation(mContext);
    }

    @Override
    public void onGetAllHdWalletCaches(List<HdWalletCache> datas) {

        if (datas != null) {
            for (HdWalletCache hdWalletCache : datas) {
                if (hdWalletCache.getAddress().equals(hdWallet.getAddress())) {
                    KProgressHUD.create(mContext)
                            .setCustomView(mContext, R.drawable.tips_error)
                            .setLabel(getString(R.string.tmperson_wallet_exsits))
                            .show().setDuration(1500);
                    return;
                }
            }
        }

        // 给新增钱包取名
        int num = datas == null ? 1 : datas.size() + 1;
        hdWallet.setWalletName(hdWallet.getCoinType() + "-" + num);
        mPresenter.addHdWalletCache(hdWallet);
    }



}
